package com.sample.wanandroidx.basic

import android.os.Bundle
import androidx.databinding.ViewDataBinding
import androidx.databinding.library.baseAdapters.BR
import androidx.lifecycle.ViewModel
import com.sample.library.BaseViewModel
import com.sample.library.ext.ActivityViewBindingInflater
import com.sample.library.util.ClassUtil

/**
 * 描述：Mvvm Activity基类
 *
 * @author 张钦
 */
abstract class BaseMvvmActivity<T : ViewDataBinding, VM : BaseViewModel>(bindingInflater: ActivityViewBindingInflater<T>) :
    BaseActivity<T>(bindingInflater) {

    //XML里面导入的命名一定要是viewModel
    protected lateinit var viewModel: VM

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        viewModel = getActivityViewModel(ClassUtil.getViewModel(this))
        viewModel.iView = this

        binding.setVariable(BR.viewModel, viewModel)
        binding.lifecycleOwner = this
        binding.executePendingBindings()

        initializeMvvm()
    }

    //网络恢复时自动重试重试
    override fun onRetry() {
        initializeMvvm()
    }

    final override fun initialization() {

    }

    protected open fun <T : ViewModel> getActivityViewModel(modelClass: Class<T>): T {
        return defaultViewModelProviderFactory.create(modelClass)
    }

    abstract fun initializeMvvm()

}